package com.hongshan.wallet.core.home

import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.content.Intent
import android.content.pm.ActivityInfo
import android.databinding.ViewDataBinding
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import com.alibaba.android.arouter.facade.annotation.Route
import com.hongshan.wallet.BR
import com.hongshan.wallet.R
import com.hongshan.wallet.base.MyActivity
import com.hongshan.wallet.router.RouterIndex
import com.hongshan.wallet.util.Glide4Engine
import com.zhihu.matisse.Matisse
import com.zhihu.matisse.MimeType
import kotlinx.android.synthetic.main.activity_top_up.*
import kotlinx.android.synthetic.main.appbar.*





/**
 * Copyright Zhangxx.
 * Copyright (c) 2019
 * All rights reserved
 *
 * @project  : Wallet
 * @package  : com.example.wallet.core.home
 * @author   : zhangxx
 * @date     : 2019/4/10
 * @email    : qq957413212@hotmail.com
 * @desc     : //TODO
 */
@Route(path = RouterIndex.TOP_UP)
class TopUpActivity: MyActivity<ViewDataBinding, TopUpViewModel>() {

    override fun initVariableId() = BR.vm

    override fun initContentView(savedInstanceState: Bundle?) = R.layout.activity_top_up

    override fun initViewModel() = ViewModelProviders.of(this)[TopUpViewModel::class.java]

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        tvTitle.text = "充值"
        viewModel.selectPic.observe(this, Observer { _ -> getPicture() })
        val word = "云 闪 付 , 银 行 APP , 网 银"
        val string = SpannableString("1. 请 使 用 $word 等 转 账 到 上 述 银 行 卡 ;")
        val foregroundColorSpan = ForegroundColorSpan(resources.getColor(R.color.colorPrimary))
        val start = string.indexOf(word)
        string.setSpan(foregroundColorSpan, start, start+word.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
        tvTopUpDesc.text = string

    }

    fun getPicture() {
        Matisse.from(this)
            .choose(MimeType.ofImage())
            .countable(true)
            .maxSelectable(1)
            .restrictOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED)
            .thumbnailScale(0.85f)
            .imageEngine(Glide4Engine)
            .forResult(REQUEST_CODE_PICTURE)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == REQUEST_CODE_PICTURE && resultCode == RESULT_OK) {
            val result = Matisse.obtainPathResult(data)
            if (result.isNotEmpty()) {
                viewModel.screenPic.set(result[0])
            }
        }
    }

    companion object {
        const val REQUEST_CODE_PICTURE = 10120
    }
}